﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //1创建数据上下文(增删改查)
            Model1 model = new Model1();
            //2属性导航
            var task = model.Task;
            //3数据操作
            var list = task.Where(p => p.Content.Contains("实现")).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.TaskID + "\t" + item.Content);
            }
            //3.2添加
            var t = new Task()
            {
                AddTime = DateTime.Now,
                Content = "实现添加功能",
                TaskPriority = 1,
                uid = 1,
                TaskState = 1
            };
            task.Add(t);
            model.SaveChanges();

            //3.3删除
            /*
            1.找到要删除的对象FirstOrDefault
            2.判断查找结果 null
            3. ！=null  Remove();
            4.同步至数据库 SaveChanges();
             */
            var d = task.FirstOrDefault(p => p.TaskID == 5);
            if (d != null)
            {
                task.Remove(d);
                model.SaveChanges();
            }

            //3.4修改
            /*
            1.找到要删除的对象FirstOrDefault
            2.判断查找结果 null
            3. ！=null  对属性重新赋值;       
            4.同步至数据库 SaveChanges();
             */
            var u = task.FirstOrDefault(p => p.TaskID == 1);
            if (u != null)
            {
                u.TaskState = 2;
                u.FinishedTime = DateTime.Now;
                model.SaveChanges();
            }
            Console.ReadKey();
        }
    }
}
